/**
 * Program  : BaseActivity.java
 * Author   : qianj
 * Create   : 2012-5-24 下午04:07:58
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package cn.learn.base;

import java.util.Map;
import java.util.Set;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;

/**
 * 基本Activity,有顶部标题栏
 * @author   qianj
 * @version  1.0.0
 * @2012-5-24 下午04:07:58
 */
public abstract class BaseFrameActivity extends ActivityGroup implements OnClickListener,OnCheckedChangeListener{
	
	protected BaseFrameLayout ly;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setView();
	}

	@Override
	public void onClick(View view) {
		Button leftButton = this.ly.leftButton;
		Button rightButton = this.ly.rightButton;
		if (view == leftButton) {
			handleTitleBarEvent(0);
		} else if (view == rightButton) {
			handleTitleBarEvent(1);
		}
	}
	
	//需要重写的方法
	protected abstract void handleTitleBarEvent(int buttonId);
	
	//设置标题
	protected void SetTitle(String title) {
		if (ly != null) {
			ly.setTitle(title);
		}
	}
	
	//设置titleBar左右图片或文字  设置button的背景图要么文字
	protected void setTitleBar(int leftBtnDrawId, String leftBtnText,
			int rightBtnDrawId, String rightBtnText) {
		if (leftBtnDrawId > 0 || leftBtnText.length() > 0) {
			ly.leftButton.setVisibility(View.VISIBLE);
			if (leftBtnDrawId > 0) {
				ly.leftButton.setBackgroundResource(leftBtnDrawId);
			}
			if (leftBtnText.length() > 0) {
				ly.leftButton.setText(leftBtnText);
			}
		} else {
			ly.leftButton.setVisibility(View.INVISIBLE);
		}
		if (rightBtnDrawId > 0 || rightBtnText.length() > 0) {
			ly.rightButton.setVisibility(View.VISIBLE);
			if (rightBtnDrawId > 0) {
				ly.rightButton.setBackgroundResource(rightBtnDrawId);
			}
			if (rightBtnText.length() > 0) {
				ly.rightButton.setText(rightBtnText);
			}
		} else {
			ly.rightButton.setVisibility(View.INVISIBLE);
		}
	}
	
	//设置界面布局
	private void setView() {
		ly = new BaseFrameLayout(this);
		setContentView(ly);
		ly.leftButton.setOnClickListener(this);
		ly.rightButton.setOnClickListener(this);
		ly.tabHost.setup(this.getLocalActivityManager());
		ly.group.setOnCheckedChangeListener(this);
	}
	
	/**
	 * 设置tabhost跳转后的界面
	 * @author qianj
	 * @create 2012-5-25 下午12:06:55
	 * @since 
	 * @param map
	 */
	protected void setTabHost(Map<String,Intent> map) {
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			TabSpec tab = ly.tabHost.newTabSpec(key);
			tab.setIndicator(key).setContent(map.get(key));
			ly.tabHost.addTab(tab);
		}
		ly.tabHost.setCurrentTab(0);
		RadioButton rb = (RadioButton) ly.group.getChildAt(0);
		rb.setChecked(true);
	}
	
	
	protected void setCurrentTab(String tag) {
		ly.tabHost.setCurrentTabByTag(tag);
	}
	
	
	protected void showToast(String text) {
		Toast localToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
		//localToast.setGravity(17, 0, 0);
		localToast.show();
	}
	
	
	
	
}

